package org.openmore.platform.controller;

import io.swagger.annotations.*;
import me.chanjar.weixin.common.bean.menu.WxMenu;
import me.chanjar.weixin.mp.bean.menu.WxMpMenu;
import me.chanjar.weixin.mp.bean.result.WxMpUser;
import org.openmore.platform.dto.common.BaseResponse;
import org.openmore.platform.wechatMp.WechatMpService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import static org.springframework.http.MediaType.APPLICATION_JSON_UTF8_VALUE;

/**
 * 微信公众号接口
 * 参考：
 * @author <a href="https://github.com/binarywang">Binary Wang</a>
 */
@Api(value = "/WechatMp", tags = "WechatMp", description = "公众号相关接口")
@RequestMapping(value = "/api/wechatMp", produces = {APPLICATION_JSON_UTF8_VALUE})
@RestController
public class WxMpController extends BaseController{
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private WechatMpService wechatMpService;

    /**
     * 接受微信服务器认证消息
     */
    @ApiOperation(value = "接受微信服务器认证消息", response = String.class)
    @ApiResponses(value = {@ApiResponse(code = 4000, message = "请求失败：原因", response = String.class)})
    @RequestMapping(method = RequestMethod.GET, value = "/{tenantId}/authGet", produces = "text/plain;charset=utf-8")
    public String authGet(@PathVariable @ApiParam(name = "应用ID") String tenantId,
                          @RequestParam(name = "signature", required = false) String signature,
                          @RequestParam(name = "timestamp", required = false) String timestamp,
                          @RequestParam(name = "nonce", required = false) String nonce,
                          @RequestParam(name = "echostr", required = false) String echostr) {
        return wechatMpService.checkSignature(tenantId, signature, timestamp, nonce, echostr);
    }
    /**
     * 接受微信请求消息
     */
    @ApiOperation(value = "接受微信请求消息", response = String.class)
    @ApiResponses(value = {@ApiResponse(code = 4000, message = "请求失败：原因", response = String.class)})
    @RequestMapping(method = RequestMethod.POST, value = "/{tenantId}/authGet", produces = "text/plain;charset=utf-8")
    public String handleMessage(@PathVariable @ApiParam(name = "应用ID") String tenantId,
                                @RequestBody String requestBody,
                                @RequestParam(name = "msg_signature", required = false) String openId,
                                @RequestParam(name = "msg_signature", required = false) String msgSignature,
                                @RequestParam(name = "encrypt_type", required = false) String encryptType,
                                @RequestParam(name = "signature", required = false) String signature,
                                @RequestParam("timestamp") String timestamp,
                                @RequestParam("nonce") String nonce) {
        return wechatMpService.handlePost(tenantId, requestBody, signature, timestamp, nonce, openId, encryptType,
                msgSignature);
    }

    @ApiOperation(value = "获取授权信息", response = String.class)
    @ApiResponses(value = {@ApiResponse(code = 4000, message = "请求失败：原因", response = WxMpUser.class)})
    @RequestMapping(method = RequestMethod.GET, value = "/{tenantId}/greet/{code}")
    public BaseResponse greetUser(@PathVariable @ApiParam(name = "应用ID") String tenantId,
                                  @PathVariable @ApiParam("code") String code) {

        return BaseResponse.buildSuccessResponse(wechatMpService.greetUser(tenantId, code));
    }

    @ApiOperation(value = "查询公众号页面自定义菜单", response = String.class)
    @ApiResponses(value = {@ApiResponse(code = 4000, message = "请求失败：原因", response = WxMpMenu.class)})
    @RequestMapping(method = RequestMethod.GET, value = "/{tenantId}/menu")
    public BaseResponse getMenu(@PathVariable @ApiParam(name = "应用ID") String tenantId){
        return BaseResponse.buildSuccessResponse(wechatMpService.menuGet(tenantId));
    }

    @ApiOperation(value = "删除公众号页面底部菜单", response = String.class)
    @ApiResponses(value = {@ApiResponse(code = 4000, message = "请求失败：原因", response = String.class)})
    @RequestMapping(method = RequestMethod.DELETE, value = "/{tenantId}/menu")
    public BaseResponse menuDelete(@PathVariable @ApiParam(name = "应用ID") String tenantId){
        wechatMpService.menuDelete(tenantId);
        return BaseResponse.buildSuccessResponse("删除成功");
    }

    @ApiOperation(value = "根据菜单ID删除自定义公众号页面底部菜单", response = String.class)
    @ApiResponses(value = {@ApiResponse(code = 4000, message = "请求失败：原因", response = String.class)})
    @RequestMapping(method = RequestMethod.DELETE, value = "/{tenantId}/menu/{menuId}")
    public BaseResponse menuDelete(@PathVariable @ApiParam(name = "应用ID") String tenantId,
                                   @PathVariable String menuId){
        wechatMpService.menuDelete(menuId);
        return BaseResponse.buildSuccessResponse("删除成功");
    }

    @ApiOperation(value = "创建公众号页面底部按钮", response = String.class)
    @ApiResponses(value = {@ApiResponse(code = 4000, message = "请求失败：原因", response = String.class)})
    @RequestMapping(method = RequestMethod.POST, value = "/{tenantId}/menu")
    public BaseResponse createMenu(@PathVariable @ApiParam(name = "应用ID") String tenantId,
                                   @RequestBody WxMenu menu) {

        return BaseResponse.buildSuccessResponse(wechatMpService.menuCreate(tenantId, menu));
    }

    @ApiOperation(value = "自定义json创建公众号页面底部按钮", response = String.class)
    @ApiResponses(value = {@ApiResponse(code = 4000, message = "请求失败：原因", response = String.class)})
    @RequestMapping(method = RequestMethod.POST, value = "/{tenantId}/menuFromJson")
    public BaseResponse createMenu(@PathVariable @ApiParam(name = "应用ID") String tenantId,
                                   @RequestBody @ApiParam("menuJson") String menuJson) {
        return BaseResponse.buildSuccessResponse(wechatMpService.menuCreate(tenantId, menuJson));
    }
}